package com.example.myapplication2;




import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;

public class MainActivity extends AppCompatActivity{
    private Button btn1,btn2;
    private String path="https://www.wanandroid.com/user/login";
    private EditText et1,et2;
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg){
            try{
                if(msg.what==1) {
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Uri uri = Uri.parse("https://www.wanandroid.com");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };

    private String login(String name,String pwd){
        try {

            URL url = new URL(path);
            HttpURLConnection cn = (HttpURLConnection)url.openConnection();
            cn.setRequestMethod("POST");
            cn.setDoOutput(true);
            cn.setUseCaches(false);
            String str = "username="+ URLEncoder.encode(name,"utf-8")+"&password="+ URLEncoder.encode(pwd,"utf-8");
            OutputStream outputStream = cn.getOutputStream();
            outputStream.write(str.getBytes());
            outputStream.flush();
            if(cn.getResponseCode()==200){
                InputStream inputStream = cn.getInputStream();
                String s = stream2String(inputStream);
                return s;
            }
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return null;
    }
    private String stream2String(InputStream inputStream){
        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        try{
            while((line = bf.readLine())!=null){
                stringBuffer.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText) findViewById(R.id.editTextTextPersonName);
        et2 = (EditText) findViewById(R.id.editTextTextPersonName2);
        btn1 =(Button) findViewById(R.id.button);
        btn2 = (Button) findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et1.getText().toString();
                String pwd = et2.getText().toString();
                new Thread() {
                    @Override
                    public void run() {
                        String s = login(name, pwd);
                        try {
                            System.out.println(s);
                            JSONObject object = new JSONObject(s);
                            int errorCode = object.optInt("errorCode");
                            String errorMsg = object.optString("errorMsg");
                            Message msg = new Message();
                            if (errorCode == 0) {
                                msg.what = 1;
                            } else {
                                msg.what = 0;
                            }
                            msg.obj = errorMsg;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });

    }
}
